using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "DungeonLevel_", menuName = "游戏SO文件/地牢/地牢级别")]
public class DungeonLevelSO : ScriptableObject
{
    #region Header BASIC LEVEL DETAILS

    [Space(10)]
    [Header("基本级别详细信息")]

    #endregion Header BASIC LEVEL DETAILS

    #region Tooltip

    [Tooltip("级别的名称")]

    #endregion Tooltip

    public string levelName;

    #region Header ROOM TEMPLATES FOR LEVEL

    [Space(10)]
    [Header("关卡的房间模板")]

    #endregion Header ROOM TEMPLATES FOR LEVEL

    #region Tooltip

    [Tooltip("在列表中填充您希望成为关卡一部分的房间模板。 您需要确保在楼层的房间节点图中指定的所有房间节点类型都包含房间模板。")]

    #endregion Tooltip

    public List<RoomTemplateSO> roomTemplateList;

    #region Header ROOM NODE GRAPHS FOR LEVEL

    [Space(10)]
    [Header("级别的房间节点图")]

    #endregion Header ROOM NODE GRAPHS FOR LEVEL

    #region Tooltip

    [Tooltip("使用房间节点 grpahs 填充此列表，应从中随机选择用于级别的 grpah。")]

    #endregion Tooltip

    public List<RoomNodeGraphSO> roomNodeGraphList;

    #region Validation

#if UNITY_EDITOR

    // 验证已实现的可编程对象详细信息
    private void OnValidate()
    {
        HelperUtilities.ValidateCheckEmptyString(this, nameof(levelName), levelName);
        if (HelperUtilities.ValidateCheckEnumerableValues(this, nameof(roomTemplateList), roomTemplateList))
            return;
        if (HelperUtilities.ValidateCheckEnumerableValues(this, nameof(roomNodeGraphList), roomNodeGraphList))
            return;

        // 检查以确保为
        // 指定节点图

        // 首先检查是否指定了南北走廊、东/西走廊和入口类型
        bool isEWCorridor = false;
        bool isNSCorridor = false;
        bool isEntrance = false;

        // 遍历所有房间模板，以检查是否已指定此节点类型
        foreach (RoomTemplateSO roomTemplateSO in roomTemplateList)
        {
            if (roomTemplateSO == null)
                return;

            if (roomTemplateSO.roomNodeType.isCorridorEW)
                isEWCorridor = true;

            if (roomTemplateSO.roomNodeType.isCorridorNS)
                isNSCorridor = true;

            if (roomTemplateSO.roomNodeType.isEntrance)
                isEntrance = true;
        }

        if (isEWCorridor == false)
        {
            Debug.Log("In " + this.name.ToString() + " : 不是 E/W 走廊房型指定");
        }

        if (isNSCorridor == false)
        {
            Debug.Log("In " + this.name.ToString() + " : 不是 N/S 走廊房型指定");
        }

        if (isEntrance == false)
        {
            Debug.Log("In " + this.name.ToString() + " : 不是入口走廊 指定房型");
        }

        // 遍历所有节点图
        foreach (RoomNodeGraphSO roomNodeGraph in roomNodeGraphList)
        {
            if (roomNodeGraph == null)
                return;

            // 遍历节点图中的所有节点
            foreach (RoomNodeSO roomNodeSO in roomNodeGraph.roomNodeList)
            {
                if (roomNodeSO == null)
                    continue;

                // 检查是否已为每个 roomNode 类型指定了房间模板

                // 走廊和入口已经检查过
                if (roomNodeSO.roomNodeType.isEntrance || roomNodeSO.roomNodeType.isCorridorEW ||
                    roomNodeSO.roomNodeType.isCorridorNS || roomNodeSO.roomNodeType.isCorridor ||
                    roomNodeSO.roomNodeType.isNone)
                    continue;

                bool isRoomNodeTypeFound = false;

                // 遍历所有房间模板，以检查是否已指定此节点类型
                foreach (RoomTemplateSO roomTemplateSO in roomTemplateList)
                {
                    if (roomTemplateSO == null)
                        continue;

                    if (roomTemplateSO.roomNodeType == roomNodeSO.roomNodeType)
                    {
                        isRoomNodeTypeFound = true;
                        break;
                    }
                }

                if (!isRoomNodeTypeFound)
                    Debug.Log("In " + this.name.ToString() + " : 无房间模板 " +
                              roomNodeSO.roomNodeType.name.ToString() + " 为节点图找到 " +
                              roomNodeGraph.name.ToString());
            }
        }
    }

#endif

    #endregion Validation
}